#coding:utf-8

"""
    asc = 字节字符串
    byte = 字节序列
    str = 显示用的字节码字符串
"""

#ASCII码转义为16进制
def asc2byte(asc_str):
    return [ord(i) for i in asc_str]
        
#16进制转化为ASCII码
def byte2asc(ldata):
    asc_str = ''.join([chr(i) for i in ldata])
    return asc_str.strip()

# 二进制字符串转为文本字符串
def asc2str(ascstr):
    return ' '.join([ch.encode('hex') for ch in ascstr])

# 文本字符串转为数据序列
def str2byte(logstr):
    l = logstr.split(' ')
    return [int(i, 16) for i in l]

# 数据序列转为文本字符串
def byte2str(data):
    return ' '.join(['%02x '%i for i in data])

# 文本字符串转为二进制字符串
def str2asc(logstr):
    l = logstr.split(' ')
    return ''.join([ch.decode('hex') for ch in l])


class _Data:
    def __init__(self, data=[]):
        self._data = data
        self._cursor = 0
    def get(self, nums = 1):
        kids = self._data[self._cursor:self._cursor+nums]
        self._cursor = self._cursor + nums
        if len(kids) == 1:
            return kids[0]
        else:
            return kids
    def set(self, *args):
        for value in args:
            if isinstance(value, list):
                self._data = self._data + value
            else:
                self._data.append(value)   
    def out(self):
        return self._data
        
# 对一个序列的包装
def pickUps(data = []):
    """
        对序列的一种包装方式.
    """
    assert isinstance(data, list)
    return _Data(data)


class _Value:
    def __init__(self, value):
        self.value = value
    def get(self, begin, end):
        de = self.value >> (7- end)
        return de & (2**(end-begin+1)-1)
    def set(self, value, begin, end):
        value = value * (2 **(7-end))
        de = 0xff -2**(8-begin) + 2**(7-end)
        self.value = self.value & de | value
    def out(self):
        return self.value


#对一个字节的包装  
def pickUp(value = 0x0):
    """ 对一个字节的一种包装方式. """
    assert isinstance(value, int)
    assert value >= 0x0 and value <= 0xff
    return _Value(value)


if __name__ == '__main__':
    print str2byte("22 33 44 55 66")
